import Vue from 'vue'
import Router from 'vue-router'
import i18n from '@/lang'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
// hidden：菜单不需要显示该项时，加入hidden:true
// 如果某个配制的children只 一个菜单显示项时，不会显示二级菜单，多个菜单显示项时就会显示二级菜单
/*
 权限管理：目标：将用户信息中的权限数据应用到路由配制中去
    1：将路由配制分成二块
         a:静态路由（不需要权限管理的路由：login,404,首页），默认只应该有静态路由
         b:动态路由（需要权限管理的路由）
            在获取到用户信息后，拿到权限数据与动态路由做匹配，将匹配到的路由添加到路由配制
            在动态路由中创建与权限标识共同的数据部分
        menus=['123']   otherRoutes=[{path:'aaa',xxx:'456'},{path:'bbb',xxx:'123'}]

*/
export const otherRoutes = [
  {
    path: '/employees',
    component: Layout,
    meta: {
      code: 'employees'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Employees',
        component: () => import('@/views/employees/index'),
        meta: { title: i18n.t('route.employees'), icon: 'people' }
      },
      {
        path: 'import', // /employees/import
        name: 'Import',
        component: () => import('@/views/employees/import.vue'),
        hidden: true
      },
      {
        path: 'look/:id',
        name: 'Look',
        component: () => import('@/views/employees/look.vue'),
        hidden: true
      }
    ]
  },
  {
    path: '/setting',
    component: Layout,
    meta: {
      code: 'settings'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Setting',
        component: () => import('@/views/setting/index'),
        meta: { title: '公司设置', icon: 'setting' }
      }
    ]
  },
  {
    path: '/departments',
    component: Layout,
    meta: {
      code: 'departments'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Departments',
        component: () => import('@/views/departments/index'),
        meta: { title: '组织架构', icon: 'tree' }
      }
    ]
  },
  {
    path: '/salarys',
    component: Layout,
    meta: {
      code: 'salarys'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Salarys',
        component: () => import('@/views/salarys/index'),
        meta: { title: '工资', icon: 'money' }
      }
    ]
  },
  {
    path: '/social',
    component: Layout,
    meta: {
      code: 'social_securitys'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Social',
        component: () => import('@/views/social/index'),
        meta: { title: '社保', icon: 'table' }
      }
    ]
  },
  {
    path: '/attendances',
    component: Layout,
    meta: {
      code: 'attendances'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Attendances',
        component: () => import('@/views/attendances/index'),
        meta: { title: '考勤', icon: 'skill' }
      }
    ]
  },
  {
    path: '/approvals',
    component: Layout,
    meta: {
      code: 'approvals'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Approvals',
        component: () => import('@/views/approvals/index'),
        meta: { title: '审批', icon: 'tree-table' }
      }
    ]
  },
  {
    path: '/permission',
    component: Layout,
    meta: {
      code: 'permissions'
    },
    children: [
      {
        path: '', //   相对路由，相对于父级
        name: 'Permission',
        component: () => import('@/views/permission/index'),
        meta: { title: '权限', icon: 'lock' }
      }
    ]
  }
]
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    // meta: { title: '登录', icon: 'dashboard' }
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    // meta: { title: '404', icon: 'dashboard' }
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: i18n.t('route.dashboard'), icon: 'dashboard' }
      }
    ]
  }
  // ...otherRoutes,
  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  // 重置路由
  router.matcher = newRouter.matcher // reset router
}

export default router
